#
# @lc app=leetcode.cn id=503 lang=python3
#
# [503] 下一个更大元素 II
#

# @lc code=start
class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        ans = [-1]*len(nums)
        s = []
        for j in range(len(nums)-1, -1, -1):
            while s and s[-1] <= nums[j]:
                s.pop()
            if s:
                ans[j] = s[-1]
            s.append(nums[j])
        for j in range(len(nums)-1, -1, -1):
            while s and s[-1] <= nums[j]:
                s.pop()
            if s and ans[j]==-1:
                ans[j] = s[-1]
            s.append(nums[j])
        return ans
# @lc code=end

